package com.androidcat.yucaiedu.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.RadioButton;
import android.widget.TextView;

import com.androidcat.acnet.entity.MenuItm;
import com.androidcat.yucaiedu.AppData;
import com.androidcat.yucaiedu.R;
import com.androidcat.yucaiedu.ui.listener.OnMenuCheckedListener;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by androidcat on 2018/12/28.
 */

public class SaMenuAdapter extends BaseExpandableListAdapter {
    private Context mcontext;
    List<MenuItm> parentMenu = AppData.parentSaMenu;
    List<ArrayList<MenuItm>> childMenu = AppData.childSaMenu;
    public OnMenuCheckedListener onMenuCheckedListener;

    @Override
    // 获取分组的个数
    public int getGroupCount() {
        return parentMenu.size();
    }

    //获取指定分组中的子选项的个数
    @Override
    public int getChildrenCount(int groupPosition) {
        return childMenu.get(groupPosition).size();
    }

    //获取指定的分组数据
    @Override
    public Object getGroup(int groupPosition) {
        return parentMenu.get(groupPosition);
    }

    //获取指定分组中的指定子选项数据
    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return childMenu.get(groupPosition).get(childPosition);
    }

    //获取指定分组的ID, 这个ID必须是唯一的
    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    //获取子选项的ID, 这个ID必须是唯一的
    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    //分组和子选项是否持有稳定的ID, 就是说底层数据的改变会不会影响到它们
    @Override
    public boolean hasStableIds() {
        return true;
    }
    /**
     *
     * 获取显示指定组的视图对象
     *
     * @param groupPosition 组位置
     * @param isExpanded 该组是展开状态还是伸缩状态
     * @param convertView 重用已有的视图对象
     * @param parent 返回的视图对象始终依附于的视图组
     */
// 获取显示指定分组的视图
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        GroupViewHolder groupViewHolder;
        if (convertView == null){
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.menu_1_item,parent,false);
            groupViewHolder = new GroupViewHolder();
            groupViewHolder.tvTitle = (TextView)convertView.findViewById(R.id.menuTxt);
            convertView.setTag(groupViewHolder);
        }else {
            groupViewHolder = (GroupViewHolder)convertView.getTag();
        }
        groupViewHolder.tvTitle.setText(parentMenu.get(groupPosition).dictLabel);
        return convertView;
    }
    /**
     *
     * 获取一个视图对象，显示指定组中的指定子元素数据。
     *
     * @param groupPosition 组位置
     * @param childPosition 子元素位置
     * @param isLastChild 子元素是否处于组中的最后一个
     * @param convertView 重用已有的视图(View)对象
     * @param parent 返回的视图(View)对象始终依附于的视图组
     * @return
     * @see android.widget.ExpandableListAdapter#getChildView(int, int, boolean, View,
     *      ViewGroup)
     */

    //取得显示给定分组给定子位置的数据用的视图
    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ChildViewHolder childViewHolder;
        if (convertView==null){
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.menu_2_item,parent,false);
            childViewHolder = new ChildViewHolder();
            childViewHolder.tvTitle = convertView.findViewById(R.id.subMenuTxt);
            convertView.setTag(childViewHolder);

        }else {
            childViewHolder = (ChildViewHolder) convertView.getTag();
        }
        final MenuItm textMenu = (MenuItm) getChild(groupPosition, childPosition);
        childViewHolder.tvTitle.setText(textMenu.dictLabel);

        if (textMenu.isChecked){
            childViewHolder.tvTitle.setChecked(true);
        }else {
            childViewHolder.tvTitle.setChecked(false);
        }
        //当RadioButton被选中时，将其状态记录进States中，并更新其他RadioButton的状态使它们不被选中
        childViewHolder.tvTitle.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                for(List<MenuItm> list : childMenu){
                    for (MenuItm menu : list){
                        menu.isChecked = false;
                    }
                }
                textMenu.isChecked = true;
                if (onMenuCheckedListener != null){
                    onMenuCheckedListener.onMenuChecked(textMenu);
                }
                SaMenuAdapter.this.notifyDataSetChanged();
            }
        });

        return convertView;
    }

    //指定位置上的子元素是否可选中
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    static class GroupViewHolder {
        TextView tvTitle;
    }

    static class ChildViewHolder {
        RadioButton tvTitle;

    }

    public void setOnMenuCheckedListener(OnMenuCheckedListener onMenuCheckedListener) {
        this.onMenuCheckedListener = onMenuCheckedListener;
    }
}